package com.owen.system.keep.consumption.controller;

import com.owen.common.utils.PageUtils;
import com.owen.common.utils.Query;
import com.owen.common.utils.R;
import com.owen.system.keep.account.vo.KeepAccountVO;
import com.owen.system.keep.consumption.dto.KeepConsumptionTypeDTO;
import com.owen.system.keep.consumption.service.KeepConsumptionTypeService;
import com.owen.system.keep.consumption.vo.KeepConsumptionTypeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "收支类型")
@RequestMapping("/keep/keepConsumptionType")
public class KeepConsumptionTypeController {

    @Autowired
    private KeepConsumptionTypeService keepConsumptionTypeService;

    @GetMapping("/add/{keepAccountId}")
    @RequiresPermissions("keep:consumptionType:add")
    @ApiOperation(value = "进入新增收支类型页面", notes = "进入新增收支类型页面", httpMethod = "GET")
    String add(@PathVariable("keepAccountId") String keepAccountId, Model model) {
        model.addAttribute("keepAccountId", keepAccountId);
        return "keep/keepConsumptionType/add";
    }

    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("keep:consumptionType:add")
    @ApiOperation(value = "新增收支类型", notes = "新增收支类型", httpMethod = "POST")
    public R save(KeepConsumptionTypeDTO dto) {
        if (keepConsumptionTypeService.save(dto) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @GetMapping(value = "/listPage/{keepAccountId}")
    @RequiresPermissions("keep:consumptionType:list")
    @ApiOperation(value = "进入收支类型列表页面", notes = "进入收支类型列表页面", httpMethod = "GET")
    String listPage(@PathVariable("keepAccountId") String keepAccountId, Model model) {
        model.addAttribute("keepAccountId", keepAccountId);
        return "keep/keepConsumptionType/list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("keep:consumptionType:list")
    @ApiOperation(value = "收支类型列表数据", notes = "收支类型列表数据", httpMethod = "GET")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<KeepConsumptionTypeVO> KeepConsumptionTypeList = keepConsumptionTypeService.list(query);
        int total = keepConsumptionTypeService.count(query);
        PageUtils pageUtils = new PageUtils(KeepConsumptionTypeList, total);
        return pageUtils;
    }

    @ResponseBody
    @RequiresPermissions("keep:consumptionType:list")
    @ApiOperation(value = "某账户下的收/支类型", notes = "某账户下的收/支类型", httpMethod = "GET")
    @GetMapping("/listByKeepAccountIdAndConsumptionType/{keepAccountId}/{consumptionType}")
    public List<KeepConsumptionTypeVO> listByKeepAccountIdAndConsumptionType(@PathVariable("keepAccountId") String keepAccountId, @PathVariable("consumptionType") String consumptionType) {
        return keepConsumptionTypeService.selectByKeepAccountIdAndConsumptionType(keepAccountId, consumptionType);
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("keep:consumptionType:edit")
    @ApiOperation(value = "进入收支类型编辑页面", notes = "进入收支类型编辑界面", httpMethod = "GET")
    String edit(@PathVariable("id") String id, Model model) {
        KeepConsumptionTypeVO keepConsumptionType = keepConsumptionTypeService.get(id);
        model.addAttribute("keepConsumptionType", keepConsumptionType);
        return "keep/keepConsumptionType/edit";
    }

    @ResponseBody
    @PostMapping("/update")
    @ApiOperation(value = "修改收支类型", notes = "修改收支类型", httpMethod = "POST")
    @RequiresPermissions("keep:consumptionType:edit")
    public R update(KeepConsumptionTypeDTO dto) {
        keepConsumptionTypeService.update(dto);
        return R.ok();
    }

    @ResponseBody
    @PostMapping("/remove")
    @RequiresPermissions("keep:consumptionType:remove")
    @ApiOperation(value = "删除收支类型", notes = "删除收支类型", httpMethod = "POST")
    public R remove(String id) {
        if (keepConsumptionTypeService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @PostMapping("/batchRemove")
    @RequiresPermissions("keep:consumptionType:batchRemove")
    @ApiOperation(value = "批量删除收支类型", notes = "批量删除收支类型", httpMethod = "POST")
    public R remove(@RequestParam("ids[]") String[] ids) {
        keepConsumptionTypeService.batchRemove(ids);
        return R.ok();
    }

}
